package com.ggh.smartreply;

import android.app.ActivityManager;
import android.content.Intent;

import android.content.IntentFilter;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;

import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;


public class MyActivity extends ActionBarActivity {

    private TextView textView = null;
    private CheckBox checkBox = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        textView = (TextView)findViewById(R.id.textView);
        checkBox = (CheckBox)findViewById(R.id.checkBox);
        checkBox.setOnClickListener(new CheckBoxlistener());

        Button addButton = (Button) findViewById(R.id.addButton);
        Button showButton = (Button) findViewById(R.id.showButton);

        addButton.setOnClickListener(new AddButtonListener());
        showButton.setOnClickListener(new ShowButtonListener());

        if (isServiceRunning()){
            textView.setText("\n服务已启动\n");
            checkBox.setChecked(true);

        }

    }


    public class CheckBoxlistener implements View.OnClickListener{

        @Override
        public void onClick(View view) {
            if (checkBox.isChecked()){
                textView.setText("\n服务已启动\n");
                Intent intent = new Intent();
                intent.setClass(MyActivity.this,SmartReplyService.class);
                startService(intent);
            }
            else {
                textView.setText("\n服务已停止\n");
                Intent intent = new Intent();
                intent.setClass(MyActivity.this,SmartReplyService.class);
                stopService(intent);
            }
        }
    }

    class AddButtonListener implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            Intent intent = new Intent();
            intent.setClass(MyActivity.this , AddDataBaseActivity.class);
            MyActivity.this.startActivity(intent);
        }
    }

    class ShowButtonListener implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setClass(MyActivity.this , ShowDataBaseActivity.class);
            MyActivity.this.startActivity(intent);
        }
    }

    public boolean isServiceRunning() {
        ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if ("com.ggh.smartreply.SmartReplyService".equals(service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }
}


